using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using Acms.SqlDal;

namespace Acms.Bll
{
    public class Menu : BaseItem
    {
        private Menu() : base() { }
        private Menu(Hashtable table) : base(table) { }
        private Menu(Hashtable table, List<Menu> collection) : base(table, collection) { }

        protected override Base CreateDal()
        {
            return new MenuDal();
        }
        
        public static Menu CreateInstance()
        {
            return new Menu();
        }

        private static Menu Get(Hashtable table)
        {
            if (table != null)
                return new Menu(table);
            else
                return null;
        }

        private static List<Menu> Get(Hashtable[] tables)
        {
            if (tables == null)
            {
                return null;
            }
            else
            {
                List<Menu> tmp = new List<Menu>();
                foreach (Hashtable table in tables)
                {
                    tmp.Add(new Menu(table, tmp));
                }
                return tmp;
            }
        }

        public static List<Menu> GetAll()
        {
            MenuDal dal = new MenuDal();
            return Get(dal.GetAll());
        }

        public static List<Menu> GetAllBySiteId(int siteId)
        {
            MenuDal dal = new MenuDal();
            return Get(dal.GetAllBySiteId(siteId));
        }

        
        public static Menu GetById(int id)
        {
            MenuDal dal = new MenuDal();
            return Get(dal.GetById(id));
        }

        public static Menu GetByNameAndSiteId(string name, int siteId)
        {
            MenuDal dal = new MenuDal();
            return Get(dal.GetByNameAndSiteId(name, siteId));
        }
        
        protected override void Load(Hashtable table)
        {
            id = (int)table[MenuDal.Fields.Id];
            siteId = (int)table[MenuDal.Fields.SiteId];
            name = (string)table[MenuDal.Fields.Name];
            parentId = (int)table[MenuDal.Fields.ParentId];
            parentPath = (string)table[MenuDal.Fields.ParentPath];
            includeParent = (bool)table[MenuDal.Fields.IncludeParent];
            defaultForAllPosts = (bool)table[MenuDal.Fields.DefaultForAllPosts];
        }

        private int id;
        public int Id
        {
            get { return id; }
        }

        private int siteId;

        public int SiteId
        {
            get { return siteId; }
            set { siteId = value; }
        }

        private string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        private int parentId;
        public int ParentId
        {
            get { return parentId; }
            set { parentId = value; }
        }

        private string parentPath;
        public string ParentPath
        {
            get { return parentPath; }
        }

        private bool includeParent;
        public bool IncludeParent
        {
            get { return includeParent; }
            set { includeParent = value; }
        }

        private bool defaultForAllPosts;
        public bool DefaultForAllPosts
        {
            get { return defaultForAllPosts; }
            set { defaultForAllPosts = value; }
        }

        public new void Update()
        {
            base.Update();
        }

        public new void Delete()
        {
            base.Delete();
        }

        protected override void SaveChanges(ref object concurencyId, Base dal)
        {
             ((MenuDal)dal).Update(ref id, siteId, name, parentId, ref parentPath, includeParent, defaultForAllPosts, ref concurencyId);
        }

        protected override void DeleteRecord(object concurencyId, Base dal)
        {
             ((MenuDal)dal).Delete(id, concurencyId);
        }
    }
}
